//
//  ViewController.m
//  打字软件
//
//  Created by qianfeng01 on 15-7-11.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "ViewController.h"
#define ARTICLE {for (int i = 0; i < 320; i++) {int count = arc4random()%27/26;count+=65;[article appendString:[NSString stringWithFormat:@"%c",count]];}}


@interface ViewController ()
{
//    NSMutableArray *array ;
    NSMutableString *_article;
    NSTimer *_timer;
    NSInteger _timeCount;
    UILabel *_timeLabel;
    BOOL _Flag;
}
@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
//    array = [[NSMutableArray alloc]init];
//    article = [[NSMutableString alloc]init];
    //实现一个算法，自动生成一篇文章320个字
    
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建lable，显示软件名字
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, self.view.bounds.size.width-10, 50)];
    name.textAlignment = NSTextAlignmentCenter;
    name.text = @"简易打字软件";
    name.layer.cornerRadius = 10;
    name.textColor = [UIColor blueColor];
    name.font = [UIFont boldSystemFontOfSize:20];
    name.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:name];
    
    //创建一个label，用来显示文章
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 70, self.view.bounds.size.width-10, 200)];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:17];
    label.tag = 10;
    label.backgroundColor = [UIColor orangeColor];
    label.textColor = [UIColor blackColor];
    _article = [[NSMutableString alloc]init];
    {for (int i = 0; i < 300; i++) {
        int count = arc4random()%26;
        NSLog(@"%d",count);
        count+=97;
        if (i%5==0) {
            count = 32;
        }
        [_article appendString:[NSString stringWithFormat:@"%c",count]];}
    }
//    NSLog(@"%@",_article);
    label.text = _article;
    [self.view addSubview:label];
    
    //创建UITextView用来打字
    UITextView *view = [[UITextView alloc]initWithFrame:CGRectMake(0, 340, self.view.bounds.size.width, 200)];
    view.backgroundColor = [UIColor grayColor];
    view.delegate = self;
    view.tag = 13;
    view.dataDetectorTypes = UIDataDetectorTypeNone;
    view.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:view];
    
    //创建button，用来更换文章
    UIButton *changeText = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [changeText setTitle:@"下一篇" forState:UIControlStateNormal];
    [changeText setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    changeText.frame = CGRectMake(5, 280, 100, 50);
    [changeText addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    changeText.backgroundColor = [UIColor redColor];
    [self.view addSubview:changeText];
    
    //创建label 显示打字时间
    _timeLabel  = [[UILabel alloc]initWithFrame:CGRectMake(120, 280, 50, 50)];
    _timeLabel.text = [NSString stringWithFormat:@"%ld",_timeCount];
    _timeLabel.tag = 11;
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    _timeLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_timeLabel];
    
    //创建button 用来暂停 和 重新开始
    UIButton *stopbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    stopbtn.frame = CGRectMake(180, 280, 50, 50);
    [stopbtn setTitle:@"暂停" forState:UIControlStateNormal];
    [stopbtn addTarget:self action:@selector(stopClick:) forControlEvents:UIControlEventTouchUpInside];
    stopbtn.backgroundColor = [UIColor greenColor];
    [self.view addSubview:stopbtn];
    
    
    UIButton *beginbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    beginbtn.frame = CGRectMake(240, 280, 80, 50);
    [beginbtn setTitle:@"重新开始" forState:UIControlStateNormal];
    [beginbtn addTarget:self action:@selector(beginClick:) forControlEvents:UIControlEventTouchUpInside];
    beginbtn.backgroundColor = [UIColor greenColor];
    [self.view addSubview:beginbtn];
    
    
    //创建定时器，用来计时
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(time) userInfo:nil repeats:YES];
    _timer.fireDate = [NSDate distantFuture];
    
}

- (void)stopClick:(UIButton *)button{
    _timer.fireDate = [NSDate distantFuture];
}

- (void)beginClick:(UIButton *)button{
    _timeCount = 0;
    UITextView *view = (UITextView *)[self.view viewWithTag:13];
    view.text = @"";
    _timer.fireDate = [NSDate distantFuture];
    _timeLabel.text = @"0";
    _Flag = YES;
}

- (void)change:(UIButton *)button{
    UILabel *label = (UILabel *)[self.view viewWithTag:10];

    [_article deleteCharactersInRange:NSMakeRange(0, _article.length-1)];
    {for (int i = 0; i < 300; i++) {
        int count = arc4random()%26;
        NSLog(@"%d",count);
        count+=97;
        if (i%5==0) {
            count = 32;
        }
        [_article appendString:[NSString stringWithFormat:@"%c",count]];}
    }
//    NSLog(@"%@",_article);
    label.text = _article;
}

#pragma mark - textViewdelegate

- (void)textViewDidChange:(UITextView *)textView{
//    NSLog(@"文字发生改变");
    static BOOL flag = YES;
    if (flag == YES || _Flag == YES) {
        _timer.fireDate = [NSDate distantPast];
        flag = NO;
        _Flag = NO;
    }
    if (_article.length == textView.text.length) {
        NSLog(@"编辑结束");
        _timer.fireDate = [NSDate distantFuture];
        
        //创建alertview
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Over" message:[NSString stringWithFormat:@"所用时间为 %@ s !\n 打字速度为：%d字/分 \ncome on!",_timeLabel.text,320/[_timeLabel.text intValue]*60] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
//    if(textView.text.length == 10)
//    {
//        textView.textColor = [UIColor whiteColor];
//    }
    
    
}

- (void)time{
    UILabel *label = (UILabel *)[self.view viewWithTag:11];
    _timeCount++;
    label.text = [NSString stringWithFormat:@"%ld",_timeCount];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
